/* 
 * File:   ImageRotor.cpp
 * Author: Guilherme Monteiro
 * 
 */

#include "../headers/ImageRotor.h"

ImageRotor::ImageRotor() {
}

ImageRotor::ImageRotor(const ImageRotor& orig) {
}

ImageRotor::~ImageRotor() {
}

/**
 * Rotate the image referred in 90 degrees.
 * 
 * @param img: image
 * @return rotatedImage: result image
 */
IplImage* ImageRotor::rotatePerpendicularly(IplImage* image) {   
    //take the dimention of original image
    int w = image->width;
    int h = image->height; 

    // Make a new image for the result
    CvSize newSize;
    newSize.width = cvRound(h);
    newSize.height = cvRound(w);
    IplImage *imageRotated = cvCreateImage( newSize, image->depth, image->nChannels );
    
    
    // Create a map_matrix, where the left 2x2 matrix is the transform and the right 2x1 is the dimensions.
    float m[6];
    CvMat M = cvMat(2, 3, CV_32F, m);
    
    float angleRadians = 90 * ((float)CV_PI / 180.0f);
    m[0] = (float)( cos(angleRadians) );
    m[1] = (float)( sin(angleRadians) );
    m[3] = -m[1];
    m[4] = m[0];
    m[2] = w*0.5f;  
    m[5] = h*0.5f;  


    // Transform the image
    cvGetQuadrangleSubPix( image, imageRotated, &M);

    return imageRotated;
}

/**
 * Rotate the image in <angle> degrees.
 * 
 * @param img: image
 * @param angle: angle in degrees
 * @return rotatedImage: result image
 */
IplImage* ImageRotor::rotate(IplImage* image, float angle) {
    //take the dimention of original image
    int w = image->width;
    int h = image->height; 

    // Make a new image for the result
    CvSize newSize;
    newSize.width = cvRound(w) + 10;
    newSize.height = cvRound(h) + 10;
    IplImage *imageRotated = cvCreateImage( newSize, image->depth, image->nChannels );
    
    
    // Create a map_matrix, where the left 2x2 matrix is the transform and the right 2x1 is the dimensions.
    float m[6];
    CvMat M = cvMat(2, 3, CV_32F, m);
    
    float angleRadians = angle * ((float)CV_PI / 180.0f);
    m[0] = (float)( cos(angleRadians) );
    m[1] = (float)( sin(angleRadians) );
    m[3] = -m[1];
    m[4] = m[0];
    m[2] = w*0.5f;  
    m[5] = h*0.5f;  


    // Transform the image
    cvGetQuadrangleSubPix( image, imageRotated, &M);

    return imageRotated;
}
